package cn.edu.zju.aws.ec2.demo;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest;
import com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult;
import com.amazonaws.services.ec2.model.SpotInstanceRequest;

public class getEc2 {

	private static ArrayList<String> spotInstanceRequestIds;
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		spotInstanceRequestIds=new ArrayList<String>();
		AWSCredentials credent=new ProfileCredentialsProvider("default").getCredentials();
		AmazonEC2Client ec2=new AmazonEC2Client(credent);
        Region usWest2 = Region.getRegion(Regions.US_EAST_1);
        ec2.setRegion(usWest2);
        
		spotInstanceRequestIds.add("i-32115984");
		spotInstanceRequestIds.add("i-898cc73f");
        DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest();
        describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);
        
        DescribeSpotInstanceRequestsResult describeResult = ec2.describeSpotInstanceRequests(describeRequest);
        List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests();

        // Look through each request and determine if they are all in the active state.
        for (SpotInstanceRequest describeResponse : describeResponses) {
            System.out.println(" " +describeResponse.getSpotInstanceRequestId() +
                               " is in the "+describeResponse.getState() + " state.");

        }
	}

}
